101. 对称二叉树
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//! dfs同步判断法
class Solution {
public:
bool isSymmetric(TreeNode *root) {
TreeNode *node = root;
return dfsJudge(node->left, node->right);
}
bool dfsJudge(TreeNode *nodeLeft, TreeNode *nodeRight) {
if (nodeLeft == nullptr && nodeRight == nullptr) {
return true;
} else if (nodeLeft == nullptr || nodeRight == nullptr) {
return false;
}
bool a = dfsJudge(nodeLeft->left, nodeRight->right);
bool b = dfsJudge(nodeLeft->right, nodeRight->left);
return a && b && (nodeLeft->val == nodeRight->val);
}
};